﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using BarberShop.Model;
using BarberShop.Data;
using System.Linq.Expressions;
using BarberShop.Common;

namespace BarberShop.BLL
{
    public class HairstyleInfoBLL
    {

        public static IList<HairstyleInfo> GetHairstyleList(string name)
        { 
            using(DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<HairstyleInfo, bool>> WhereExpr = PredicateExtensions.True<HairstyleInfo>();
                if (!string.IsNullOrEmpty(name))
                {
                    WhereExpr = WhereExpr.And(x => x.Name.Contains(name));
                }
                var Product = data.HairstyleInfo.Where(WhereExpr);
                return Product.ToList();
            }
        }

        public static IList<HairstyleInfo> GetHairstyleList(HairstyleInfoSearch _search)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                Expression<Func<HairstyleInfo, bool>> WhereExpr = PredicateExtensions.True<HairstyleInfo>();
                if (!string.IsNullOrEmpty(_search.Name))
                {
                    WhereExpr = WhereExpr.And(x => x.Name.Contains(_search.Name));
                }
                if (_search.StoreId!=-1)
                {
                    WhereExpr = WhereExpr.And(x => x.Sotres_ID.Equals(_search.StoreId));
                }
                if (_search.TypeId != -1)
                {
                    WhereExpr = WhereExpr.And(x => x.Type.Equals(_search.TypeId));
                }
                var Product = data.HairstyleInfo.Where(WhereExpr);
                return Product.ToList();
            }
        }
        /// <summary>
        /// 添加
        /// </summary>
        /// <param name="info"></param>
        /// <returns></returns>
        public static bool AddHairstyleInfo(HairstyleInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    data.HairstyleInfo.InsertOnSubmit(info);
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }

        }

        /// <summary>
        /// 获取
        /// </summary>
        /// <param name="id"></param>
        /// <returns></returns>
        public static HairstyleInfo GetHairstyleInfo(int id)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                var info = (from a in data.HairstyleInfo where a.ID == id select a).FirstOrDefault();
                return info;
            }
        }

        public static bool UpdateHairstyleInfo(HairstyleInfo info)
        {
            using (DataClassesDataContext data = new DataClassesDataContext())
            {
                try
                {
                    var r = (from a in data.HairstyleInfo where a.ID == info.ID select a).FirstOrDefault();
                    r.CreateDate = info.CreateDate;
                    r.Name = info.Name;
                    r.Bigimgname = info.Bigimgname;
                    r.Smallimgname = info.Smallimgname;
                    r.Type = info.Type;
                    r.Sotres_ID = info.Sotres_ID;
                    r.Description = info.Description;
                    data.SubmitChanges();
                    return true;
                }
                catch { return false; }
            }
        }
    }
}
